package com.imooc.course.business.controller.admin;

import com.imooc.course.business.form.ChapterForm;
import com.imooc.course.server.dto.ChapterDto;
import com.imooc.course.server.dto.ChapterPageDto;
import com.imooc.course.server.dto.ResponseDto;
import com.imooc.course.server.service.ChapterService;
import com.imooc.course.server.utils.CopyUtil;
import com.imooc.course.server.utils.ValidatorUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/admin/chapter")
public class ChapterController {

    private static final Logger log = LoggerFactory.getLogger(ChapterController.class);
    public static final String BUSINESS_NAME = "大章";


    @Resource(name = "chapterService")
    private ChapterService chapterService;

    /**
     * 查询列表
     * @param chapterPageDto
     * @return
     */
    @PostMapping("/list")
    public ResponseDto getList(@RequestBody ChapterPageDto chapterPageDto) {
        ValidatorUtil.notNull(chapterPageDto.getCourseId(),"课程ID");
        chapterService.getList(chapterPageDto);
        return ResponseDto.success().setContent(chapterPageDto);
    }

    /**
     * 保存，id有值为更新，id无值为新增
     * @param chapterForm
     * @return
     */
    @PostMapping("/save")
    public ResponseDto addChapter(@RequestBody ChapterForm chapterForm) {
        //入参检查
        ValidatorUtil.notNull(chapterForm.getName(), "名称");
        ValidatorUtil.notNull(chapterForm.getCourseId(), "课程ID");
        ValidatorUtil.minimumToMaximum(chapterForm.getCourseId(), "课程ID", 1, 8);

        ChapterDto chapterDto = CopyUtil.copy(chapterForm, ChapterDto.class);
        chapterService.save(chapterDto);
        return ResponseDto.success().setContent(chapterDto);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public ResponseDto delChapter(@PathVariable String id) {
        chapterService.deleteChapter(id);
        return ResponseDto.success();
    }
}
